package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.enums.ResultStatus;
import com.maigao.entity.form.AddCoachForm;
import com.maigao.entity.form.SearchCoachForm;
import com.maigao.entity.jpa.ClassRecord;
import com.maigao.service.ClassRecordService;
import com.maigao.service.CoachService;
import com.maigao.util.CollectionUtils;
import com.maigao.util.context.Context;
import com.maigao.entity.vo.*;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-21 22:57
 **/
@RestController
@RequestMapping(value = "/coach")
public class CoachController {
    @Autowired
    private CoachService coachService;
    @Autowired
    private ClassRecordService classRecordService;

    @Identify
    @ResponseBody
    @RequestMapping("listAll")
    public ResultJson<List<CoachUserVO>> listAll() {
        List<CoachUserVO> coaches = coachService.tryFindAll();
        if (CollectionUtils.isEmpty(coaches)) {
            return new ResultJson<>(ResultStatus.NO_DATA);
        }
        return ResultJson.success(coaches);
    }

    @Identify
    @ResponseBody
    @RequestMapping("add")
    public ResultJson addCoach(AddCoachForm addCoachForm) {
        Pair<Boolean, String> addResult = coachService.tryAddCoach(addCoachForm);
        if (addResult.getLeft()) {
            return ResultJson.success(addResult.getRight());
        }
        return ResultJson.fail(addResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("find")
    public ResultJson findCoach(SearchCoachForm searchCoachForm) {
        Triple<Boolean, CoachVO, String> searchResult = coachService.tryFindCoach(searchCoachForm);
        if (searchResult.getLeft()) {
            return ResultJson.success(searchResult.getMiddle());
        }
        return ResultJson.fail(searchResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("modifyMobile")
    public ResultJson updateMobile(@RequestParam Integer coachId, @RequestParam String mobile) {
        Pair<Boolean, String> updateResult = coachService.tryUpdateMobile(coachId, mobile);
        if (updateResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(updateResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findAllStudent")
    public ResultJson findAllCoachStudent() {
        Pair<Boolean, List<StudentVO>> findResult = coachService.findAllStudent();
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return new ResultJson(ResultStatus.NO_DATA);
    }

    @Identify
    @ResponseBody
    @RequestMapping("findAllStudentDetail")
    public ResultJson findAllStudent(@RequestParam String uid) {
        Pair<Boolean, List<StudentVO>> findResult = coachService.findAllStudent(uid);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return new ResultJson(ResultStatus.NO_DATA);
    }

    @Identify
    @ResponseBody
    @RequestMapping("findByStatus")
    public ResultJson<CoachVO> findAllCoaches(@RequestParam Integer status) {
        Pair<Boolean, List<CoachVO>> findResult = coachService.findAllCoachByStatus(status);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return new ResultJson(ResultStatus.NO_DATA);
    }

    @Identify
    @ResponseBody
    @RequestMapping("findFinishedClass")
    public ResultJson findFinishedClass(String coachId, @RequestParam String fromTime, @RequestParam String endTime) {
        String uid = Context.current().getUser().getUid();
        Triple<Boolean, String, List<ClassRecord>> findResult = classRecordService.findCoachFinishedClass(coachId, uid, fromTime, endTime);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findCoachFinishedClassDetail")
    public ResultJson findCoachFinishedClassDetail(String coachId, @RequestParam String fromTime, @RequestParam String endTime) {
        Triple<Boolean, String, List<CoachClassRecordDetailVO>> findResult = classRecordService.findCoachFinishedClassRecordDetail(coachId, fromTime, endTime);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    /**
     * 根据时间段查询教练已经排过的课
     *
     * @return
     */
    @Identify
    @ResponseBody
    @RequestMapping("findScheduledClass")
    public ResultJson findScheduledClass(@RequestParam Integer week) {
        Triple<Boolean, String, List<ClassRecord>> findResult = classRecordService.findScheduledClass(week);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("countFinishedClass")
    public ResultJson countFinishedClass() {
        Triple<Boolean, String, Long> findResult = classRecordService.countFinishedClass();
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findWeekClassRecord")
    public ResultJson findWeekClassRecord(@RequestParam Integer week) {
        Triple<Boolean, String, List<ClassRecordDetailVO>> findResult = classRecordService.findWeekClassRecord(week);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

}
